const mysqlHelper = require('./../db/mysql-helper.js')

/* eslint no-return-await:0 */
class TableTestModel {

  async getDeviceTypeName(args) {
    let sql = "select DeviceTypeName from Total_StaDev_Year GROUP BY DeviceTypeName"
    let result = await mysqlHelper.query(sql)
    return result
  }
  async delData(args) {

    // console.log(1111,args)
    let id=args.id
    let sql = "DELETE from Total_StaDev_Year where id= ?"
    let param=[id]
    // console.log(param)
    let result = await mysqlHelper.query(sql,param)
    // console.log(result)
    return result
  }

  async getPie(args) {

    // console.log(1111,args)
    let sql = "SELECT ORG2_NAME,count(1) as count from Total_StaDev_Year GROUP BY ORG2_NAME ORDER BY count desc"

    // console.log(param)
    let result = await mysqlHelper.query(sql)
    // console.log(result)
    return result
  }

  async updateData(args) {

    // console.log(1111,args)
    let v=args.value
    let sql = "UPDATE Total_StaDev_Year as t set t.DeviceTypeName = ? ,t.DeviceType=?,t.ORG2_NAME=?,\n" +
      "t.STA_ID=?,t.YMonth=?,t.ORG1_NAME=? where id=?"
    let param=[v.DeviceTypeName,v.DeviceType,v.ORG2_NAME,v.STA_ID,v.YMonth,v.ORG1_NAME,args.id]
    // console.log(param)
    let result = await mysqlHelper.query(sql,param)
    console.log(result)
    return result
  }

  async getDataByFilter(args) {
    let sql = "select * from Total_StaDev_Year where";
    let offset = (args.current - 1) * args.pageSize;
    let whereParam = []

    if (args.years.length == 0) {
      sql = sql+" 1=1"
    } else {
      sql = "select * from Total_StaDev_Year where YMonth in (?) "
      whereParam.push(args.years.toString())
    }
    if (args.deviceType.length == 0) {
      sql  = sql + " and 1=1"
    } else {
      sql = sql + " and DeviceTypeName in (?)";
      whereParam.push(args.deviceType.toString())
    }
    sql = sql + " limit ?,?";

    whereParam = whereParam.concat([offset, args.pageSize])
    console.log(sql)
    let result = await mysqlHelper.query(sql, whereParam)


    return result
  }

  // async getListByFilter(args) {
  //   let sql = "SELECT Id, UserName, UserPass FROM userinfo WHERE UserName like ? limit ? ,? "
  //   let whereParam
  //   if (args.where.username != null) {
  //     whereParam = "%" + args.where.username + "%"
  //   }
  //   let params = [whereParam, args.offset, args.limit]
  //   let result = await mysqlHelper.query(sql, params)
  //   return result
  // }

  async getCountByFilter(args) {
    let sql = "select count(1) from Total_StaDev_Year where"
    let whereParam = []

    if (args.years.length == 0) {
      sql = sql+" 1=1"
    } else {
      sql = "select count(1) from Total_StaDev_Year where YMonth in (?)"
      whereParam.push(args.years.toString())
    }
    if (args.deviceType.length == 0) {
      sql  = sql + " and 1=1"
    } else {
      sql = sql + " and DeviceTypeName in (?) limit 1";
      whereParam.push(args.deviceType.toString())
    }


    // let params = [yearParms, DeviceParms]

    let result = await mysqlHelper.query(sql, whereParam)
    return result
  }

  async getYearListByFilter(args) {
    let sql = "SELECT * FROM Total_StaDev_Year  WHERE  1=1 "
    if (args.ymonth != undefined) {
      sql = sql + " and YMonth = " + args.ymonth
    }

    if (args.devicetype != undefined) {
      sql = sql + " and DeviceType = " + args.devicetype
    }
    sql = sql + " limit " + args.offset + " , " + args.limit

    let result = await mysqlHelper.query(sql)
    return result
  }

  async getYearCountByFilter(args) {
    let sql = "SELECT COUNT(1) AS total FROM Total_StaDev_Year  WHERE  1=1 "
    if (args.ymonth != undefined) {
      sql = sql + " and YMonth = " + args.ymonth
    }

    if (args.devicetype != undefined) {
      sql = sql + " and DeviceType = " + args.devicetype
    }
    let result = await mysqlHelper.query(sql)
    return result
  }
}

module.exports = new TableTestModel();
